/*Program to compute inaction rates*/
/*Will also calculate inaction by size class and "weak" inaction (n +- 1%)*/
/*Revised March 2023*/

libname input "/OEUS/users/FEDSusers/QCEW/data/QuartData/";
libname output "/OEUS/users/FEDSusers/QCEW/revision_2024/Output/";
options  errors=1;

proc delete data=output.inactionrate_allq_et;
proc delete data=output.inactionrate_allq_et_wgt;
proc delete data=output.inactionrate_allq_et_over20;
proc delete data=output.inactionrate_allq_et_wgt_over20;

%macro mac(YrBeg=,YrEnd=);

%do z=&YrBeg %to &YrEnd;

        %do j=1 %to 4;
                %let qrtyr=&z&j;

	**STOP IF AFTER 2015Q3;
	%if &j>3 and &z=2015 %then %do; %end;
	%else %do;

*x gzip -d "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&z&j..sas7bdat.gz";

data intermed (keep=ldb_num emp year qrt flag inactive inactive11 inactive22 );
                	set input.ldb&z&j;
            
			rename m3_empl=emp;
			
			if own_code~=5 then delete;
			if succ_break_consol_flag~=0 or pred_break_consol_flag~=0 then delete;
			
			
  			qrt=%eval(&j);
                	year=%eval(&z);

			inactive=1;
			inactive11=1;
			inactive22=1;
			flag = 0;
			run;

proc sort data=intermed; by ldb_num;

*Merge forward 4 years of data;
%do zz=&z %to &z+4;

        %do jj=1 %to 4;

	    %if &zz=&z and &jj<=&j %then %do; %end;
	    %else %do;
	
                %let qrtyr=&zz&jj;
		
		%if &zz>=2016 %then %do; %end;
		%else %if &zz=2015 and &jj>=4 %then %do; %end;
		%else %do;

*x gzip -d "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&zz&jj..sas7bdat.gz"; 

proc sort data=input.ldb&zz&jj; by ldb_num;
      data intermed;
      merge intermed input.ldb&zz&jj (keep=ldb_num m3_empl own_code pred_break_consol_flag 
                		succ_break_consol_flag rename=(m3_empl=emplst) );
      	   by ldb_num;
	   
	   	if own_code~=5 then delete;
		if pred_break_consol_flag~=0 or succ_break_consol_flag~=0 then delete;

  		qrtlst=%eval(&jj);
                yearlst=%eval(&zz);

		
		if emp=0 or emp=. or  emplst=0 or emplst=.  then flag = 1;

		if emp~=emplst & flag=0 then inactive = 0;
		if inactive = 1 then empavg = 0.5*(emp+emplst);
		
		*Use 1 emp,1% percent inaction band;
		if emp<min(emplst-1,floor(emplst*.99)) or emp>max(emplst+1,ceil(emplst*1.01)) then flag11 = 1;
		if flag11=1 and flag=0 then inactive11=0;
		if inactive11 = 1 then empavg11 = 0.5*(emp + emplst);

		*Use 2 emp, 2% inaction band;
		if emp<min(emplst-2,floor(emplst*.98)) or emp>max(emplst+2,ceil(emplst*1.02)) then flag22 = 1;
		if flag22=1 and flag=0 then inactive22=0;
		if inactive22 = 1 then  empavg22 = 0.5*(emp + emplst);
	run;


*x gzip "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&z&j..sas7bdat.gz";
*x gzip "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&zz&jj..sas7bdat.gz"; 

proc means data=intermed noprint nway;
where flag = 0;
output out=inactionrate mean(inactive inactive11 inactive22 empavg empavg11 empavg22)=;
data inactionrate;set inactionrate;
qrt=%eval(&j);year=%eval(&z);
qrtlst=%eval(&jj);yearlst=%eval(&zz);
proc append base=output.inactionrate_allq_et data=inactionrate;

****************WEIGHTED;
proc means data=intermed noprint nway;
weight emp;
where flag = 0;
output out=inactionrate_wgt mean(inactive inactive11 inactive22 empavg empavg11 empavg22)=;

data inactionrate_wgt;set inactionrate_wgt;
qrt=%eval(&j);year=%eval(&z);
qrtlst=%eval(&jj);yearlst=%eval(&zz);

proc append base=output.inactionrate_allq_et_wgt data=inactionrate_wgt;

***************Over 20;
proc means data=intermed noprint nway;
where emp>20 and flag = 0;
output out=inactionrate_size mean(inactive inactive11 inactive22 empavg empavg11 empavg22)=;

data inactionrate_size;set inactionrate_size;
qrt=%eval(&j);year=%eval(&z);
qrtlst=%eval(&jj);yearlst=%eval(&zz);

proc append base=output.inactionrate_allq_et_over20 data=inactionrate_size;

***************Over 20 and emp-weighted;
proc means data=intermed noprint nway;
where emp>20 and flag = 0;
weight emp;
output out=inactionrate_size mean(inactive inactive11 inactive22 empavg empavg11 empavg22)=;

data inactionrate_size_wgt;set inactionrate_size;
qrt=%eval(&j);year=%eval(&z);
qrtlst=%eval(&jj);yearlst=%eval(&zz);

proc append base=output.inactionrate_allq_et_wgt_over20 data=inactionrate_size_wgt;


run;

%end;
%end;
%end;
%end;


%end;
%end;
%end;
%mend;


*Iterate through all years to compile data, calculate inaction, delete;
%mac(YrBeg=1990,YrEnd=2015);	


